package com.aaa.service.impl;

import com.aaa.entity.Function;
import com.aaa.mapper.FunctionMapper;
import com.aaa.service.FunctionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("functionService")
public class FunctionServiceImpl implements FunctionService {


    /**
     * SQL接口
     */
    @Resource
    private FunctionMapper functionMapper;


    /**
     * 查询结果
     * @return 返回出去
     */
    @Override
    public List<Function> query() {
        return functionMapper.query();
    }


    /**
     * 用户登录信息
     * @param sid
     * @return
     */
    @Override
    public Set<String> find(Integer sid) {
        return functionMapper.find(sid);
    }

    /**
     * 用户所拥有的菜单
     * @param sname
     * @return
     */
    @Override
    public List<Map> findlist(String sname) {
        /*Map aaaa=new HashMap();
        aaaa.put("sname",sname);*/
        List<Map> list=functionMapper.findlist(sname);
        List<Map> listto=new ArrayList<>();
        //遍历一级菜单
        for (Map map : list) {
            if(map.get("pid").toString().equals("0")){
                listto.add(map);
            }
        }
        List<Map> list1=new ArrayList<>();
        //遍历二级菜单
        for (Map map : listto) {
            List<Map> list2=new ArrayList<>();
            for (Map map1 : list) {
                if(map.get("id").toString().equals(map1.get("pid").toString())){
                    //将id转成string类型
                    map1.put("indexid",map1.get("id").toString());
                    //将map集合添加到list集合中
                    list2.add(map1);

                }
            }
            map.put("indexid",map.get("id").toString());
            map.put("pidd",list2);
            list1.add(map);
        }

        //输出遍历结果
       /* for (Map map : list1) {
           // System.out.println(map);
        }*/


        return list1;
    }

    /**
     * 查询全部菜单信息
     * @return
     */
    @Override
    public Object findAll() {

        List<Map<String, Object>> list=functionMapper.findAll();
        List<Map> listto=new ArrayList<>();
        List<Map> listtt=new ArrayList<>();
        //遍历一级菜单
        for (Map map : list) {
            if(map.get("pid").toString().equals("0")){
                Map mappp=new HashMap();
                mappp.put("id",map.get("id"));
                mappp.put("lable",map.get("lable"));
                listtt.add(mappp);
                listto.add(map);
            }
        }
        List<Map> list1=new ArrayList<>();
        List<Map> list1111=new ArrayList<>();
        //遍历二级菜单
        for (Map map : listto) {
            List<Map> list2=new ArrayList<>();
            List<Map> list222=new ArrayList<>();
            for (Map map1 : list) {
                if(map.get("id").toString().equals(map1.get("pid").toString())){
                    //将id转成string类型
                    map1.put("indexid",map1.get("id").toString());
                    map1.put("label",map1.get("lable").toString());
                    //将map集合添加到list集合中
                    Map mapppp=new HashMap();
                    mapppp.put("id",map1.get("id"));
                    mapppp.put("label",map1.get("lable"));
                    list222.add(mapppp);
                    list2.add(map1);
                }
            }
            map.put("indexid",map.get("id").toString());
            map.put("lable",map.get("lable").toString());
            map.put("pidd",list2);
            Map mapppo=new HashMap();
            mapppo.put("id",map.get("id"));
            mapppo.put("label",map.get("lable"));
            mapppo.put("children",list222);
            list1111.add(mapppo);
            list1.add(map);
        }

        //遍历结果
//        for (Map map : list1) {
//            System.out.println(list1);
//        }
        for (Map map : list1111) {
           // System.out.println(map);
        }
        return list1111;
    }

    /**
     * 根据id查询角色菜单信息
     * @param id 前台返回角色id
     * @return
     */
    @Override
    public Object findbyidto(Integer id) {
        List<Map> list=functionMapper.findbyidto(id);
        String[] aaa = new String[list.size()];
        for (Integer i=0;i<list.size();i++) {
            aaa[i]=list.get(i).get("id").toString();

        }
        return aaa;
    }

    /**
     * 菜单路径信息
     * @param id
     * @return
     */
    /*@Override
    public String findAllById(Integer id) {
        return functionMapper.findAllById(id);
    }*/

}
